#include "Missile.h"

BaseMissile::BaseMissile() : PhysicsBox()
{
	FuelLeft = 0;
	FuelConsumptionRate = 0;
	Activated = false;
	Thrust = 0;
	Mass = 0;
}

BaseMissile::BaseMissile(IDirect3DDevice9 *nDevice) : PhysicsBox(nDevice)
{
	MaxFuel = 0;
	FuelLeft = 0;
	FuelConsumptionRate = 0;
	Activated = false;
	Thrust = 0;
	Mass = 0;

	Trail = new MissileTrail(nDevice, "MissileEffect.fx", "Main", this->Position, 150, .01f);
}

BaseMissile::BaseMissile(IDirect3DDevice9 *nDevice, D3DXVECTOR3 nPosition, D3DXVECTOR3 nVelocity)
	: PhysicsBox(nDevice, nPosition, nVelocity)
{
	MaxFuel = 0;
	FuelLeft = 0;
	FuelConsumptionRate = 0;
	Activated = false;
	Thrust = 0;
	Mass = 0;
	Trail = new MissileTrail(nDevice, "MissileEffect.fx", "Main", this->Position, 50, .01f);
}

BaseMissile::BaseMissile(IDirect3DDevice9 *nDevice, D3DXVECTOR3 nPosition, D3DXVECTOR3 nVelocity,
						 float StartingFuel, float FuelRate, float nThrust, float nMass, float nFuseTime)
	: PhysicsBox(nDevice, nPosition, nVelocity)
{
	IsExploded = false;
	CurrentTime = 0;
	FuseTime = nFuseTime;
	MaxFuel = StartingFuel;
	FuelLeft = StartingFuel;
	FuelConsumptionRate = FuelRate;
	Activated = false;
	Thrust = nThrust;
	Mass = nMass;
	Trail = new MissileTrail(nDevice, "MissileEffect.fx", "Main", this->Position, 200, .01f);
	MainExplosion = new Explosion(nDevice, "Explosion.fx", "Main", this->Position, 2000, .0001f);
}

BaseMissile::~BaseMissile()
{
	if(Trail)
		delete Trail;

	if(MainExplosion)
		delete MainExplosion;
}

void BaseMissile::ActivateMissile( float dt )
{
	if(!IsExploded)
	{
		Activated = true;
		D3DXVECTOR4 tempInfluenceVector;
		D3DXMatrixRotationYawPitchRoll(&RotationMatrix, Angles.x, Angles.y, Angles.z);
		D3DXVec3Transform(&tempInfluenceVector, &D3DXVECTOR3(0, 0, 1), &RotationMatrix);
		AngledThrust = D3DXVECTOR3(tempInfluenceVector.x, tempInfluenceVector.y, tempInfluenceVector.z);
		D3DXVec3Normalize(&AngledThrust, &AngledThrust);
		AngledThrust *= (Thrust/Mass);
		CurrentTime = 0;

		Trail->Activate();
	}

}

bool BaseMissile::IsActive( void )
{
	return Activated;
}

bool BaseMissile::HasFuelLeft( void )
{
	return(FuelLeft > 0);
}

void BaseMissile::DeactivateMissile( void )
{
	Activated = false;
	Trail->Deactivate();
}

void BaseMissile::SetAngles(D3DXVECTOR3 nAngles)
{
	Angles = nAngles;
}

void BaseMissile::SetAngles( float nX, float nY, float nZ)
{
	Angles = D3DXVECTOR3( nX, nY, nZ);
}

D3DXVECTOR3 BaseMissile::GetAngles( void )
{
	return Angles;
}

void BaseMissile::Update(float dt)
{

	if(FuelLeft <= 0)
		DeactivateMissile();

	D3DXMatrixRotationYawPitchRoll(&RotationMatrix, Angles.x, Angles.y, Angles.z);

	if(Activated)
	{
		FuelLeft -= FuelConsumptionRate * dt;
		Velocity += AngledThrust;
		Trail->SetAngles(this->Angles);
	}

	if(Activated || Trail->GetOldActive())
		CurrentTime += dt;

	if(FuseTime < CurrentTime && !MainExplosion->GetIsActive())
	{
		Explode();
	}

	MainExplosion->Update(dt);
	if(!MainExplosion->GetIsActive())
	{
		MainExplosion->SetPosition(Position);
	}

	if(MainExplosion->GetIsActive())
	{
		Velocity = D3DXVECTOR3(0,0,0);
	}

	Trail->Update(dt);
	Trail->SetPosition(Position);

	Position += (Velocity* dt);
	
	/*//Collision box code
	D3DXVECTOR3 NewHalfVector = GetHalfVector();
	D3DXVECTOR3 OldHalfVector = GetHalfVector();
	D3DXMatrixRotationYawPitchRoll(&RotationMatrix, Angles.x, Angles.y, Angles.z);
	D3DXVec3TransformCoord(&NewHalfVector, &NewHalfVector, &RotationMatrix);
	D3DXVec3Normalize(&NewHalfVector, &NewHalfVector);
	//SetHalfVector(D3DXVECTOR3(NewHalfVector.x * OldHalfVector.x,NewHalfVector.y * OldHalfVector.y,NewHalfVector.z * OldHalfVector.z));
	SetHalfVector(NewHalfVector);
	//Collision box code*/

	BoundingBox.Position = this->Position;
	PhysicsBox::Update(dt);

		
}

void BaseMissile::Render( void )
{

	if(!IsExploded)
		PhysicsBox::Render();
	Trail->Render();
	MainExplosion->Render();
}

void BaseMissile::SetWVPMatrix(const D3DXMATRIX &Matrix)
{
	Trail->SetWVPMatrix(Matrix);
	MainExplosion->SetWVPMatrix(Matrix);
}

void BaseMissile::SetEyePosition(const D3DXVECTOR3 &nVector)
{
	Trail->SetCameraPosition(nVector);
	MainExplosion->SetCameraPosition(nVector);
}

bool BaseMissile::GetOldActive( void )
{
	return Trail->GetOldActive();
}

void BaseMissile::Reset( void )
{
	IsExploded = false;
	CurrentTime = 0;
	FuelLeft = MaxFuel;
	Activated = false;
	//Position = D3DXVECTOR3(0,0,0);
	Velocity = D3DXVECTOR3(0,0,0);
	Trail->Reset();
	MainExplosion->Reset();
}

void BaseMissile::Explode( void )
{
	if(!IsExploded)
	{
		DeactivateMissile();
		IsExploded = true;
		MainExplosion->Activate();
		SetVelocity(D3DXVECTOR3(0,0,0));
		//SetPosition(Position + D3DXVECTOR3(999999999.f, 999999999.f, 999999999.f)); 
	}
}

void BaseMissile::onLostDevice( void )
{
	Trail->onLostDevice();
	MainExplosion->onLostDevice();
}

void BaseMissile::onResetDevice( void )
{
	Trail->onResetDevice();
	MainExplosion->onResetDevice();
}

bool BaseMissile::GetIsExploded( void )
{
	return IsExploded;
}